import { App, createVNode, VNode, render } from 'vue'
import aToast from './Toast.vue'

export default {
    install(app: App) {
        // 将组件转换为虚拟DOM
        const Vnode: VNode = createVNode(aToast, {})
        // 将虚拟DOM挂载到页面节点上
        render(Vnode, document.body)
        // 将插件的主要方法挂载到 app.config.globalProperties 上，这里的 $toast 是自定义的
        app.config.globalProperties.$mesage = {

            openToast({ duration = 3000, text, icon }: { duration?: number, text: string, icon?: string }) {
                Vnode.component?.exposed?.openToast({
                    duration,
                    text,
                    icon
                })
            },
            openModal({ text, success, cancel,isCancel }: { text?: string, success?: object, cancel?: object,isCancel?:boolean }) {
                Vnode.component?.exposed?.openModal({
                    text,
                    success,
                    cancel,
					isCancel
                })
            },
        }
    },
}


// 编写自定义插件的声明文件，防止报错，声明后也会有智能提示
type Toast = {
    openToast: <T>({ duration, text, icon }: { duration?: number, text: string, icon?: string }) => void,
    openModal: <T>({ text, success, cancel,isCancel }: { text?: string, success?: object, cancel?: object,isCancel?:boolean }) => void,
}
declare module '@vue/runtime-core' {
    export interface ComponentCustomProperties {
        $mesage: Toast
    }
}  